package com.health.controller.menu;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.constant.MessageConstant;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.Menu;
import com.health.pojo.Role;
import com.health.pojo.User;
import com.health.service.MenuService;
import com.health.service.UserService;
import com.health.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Classname MenuController
 * @Description 菜单控制层
 * @Version 1.0.0
 * @Date 2023/3/17 17:29
 * @Author RainGrd
 */
@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @Reference
    private UserService userService;

    /**
     * 用于查询导航菜单
     *
     * @param userName
     * @return
     */
    @RequestMapping("/getMenu.do")
    public Result getMenu(@Param("userName") String userName) {
        log.info("userName:{}", userName);
        UserUtils userUtils = new UserUtils();
        List<Menu> menus = new ArrayList<>();
        //根据用户名获取当前用户
        User user = userService.queryByUserName(userName);
        log.info("user:{}", user);
        //根据当前用户的RoleId查询菜单
        Set<Role> roles = user.getRoles();
        for (Role role : roles) {
            log.info("role:{}", role);
            if (userUtils.hasRole(role.getKeyword())) {
                menus = menuService.queryMenuList(role.getId());
            }
        }
        return menus.size() > 0 ? Result.success(menus, MessageConstant.GET_MENU_SUCCESS) : Result.error(MessageConstant.GET_MENU_FAIL);
    }

    /**
     * 查询所有level为1的菜单
     *
     * @return
     */
    @RequestMapping("/findMenuParentMenuId.do")
    public Result findMenuParentMenuId() {
        return menuService.queryMenuByLevel();
    }

    /**
     * 分页查询预约管理
     *
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_QUERY')")//权限校验
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("queryPageBean:{}", queryPageBean);
        return menuService.queryMenuPage(queryPageBean);
    }

    /**
     * 用于保存预约管理
     *
     * @param menu
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_ADD')")//权限校验
    @RequestMapping("/add.do")
    public Result add(@RequestBody Menu menu, Integer[] roleIds) {
        return menuService.saveMenu(menu, roleIds);
    }

    @RequestMapping("/findRoleIdsByMenuId.do")
    public Result findRoleIdsByMenuId(@Param("menuId") Integer menuId) {
        log.info("menuId:{}", menuId);
        List<Integer> menuIds = menuService.queryRoleIdsByMenuId(menuId);
        log.info("menuIds:{}", menuIds);
        if (menuIds != null && menuIds.size() > 0) {
            return Result.success(menuIds, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
        }
        return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    /**
     * 编辑
     *
     * @param menu
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_EDIT')")//权限校验
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody Menu menu,Integer[] roleIds) {
        log.info("要编辑的菜单:{}", menu);
        return menuService.editMenu(menu,roleIds);
    }


    /**
     * 根据id删除预约管理
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_DELETE')")//权限校验
    @RequestMapping("/delete.do")
    public Result delete(@RequestParam("id") Integer id) {
        log.info("要删除的预约管理id:{}", id);
        return menuService.clearMenu(id);
    }

    /* *//**
     * 用于查询全部预约管理
     *
     * @return
     *//*
    @RequestMapping("/queryAll.do")
    public Result queryAll() {
        log.info(checkItemService.queryAll().toString());
        return Result.success(checkItemService.queryAll());
    }

    *//**
     * 分页查询预约管理
     *
     * @param queryPageBean
     * @return
     *//*
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("queryPageBean:{}", queryPageBean);
        return checkItemService.queryCheckItemPage(queryPageBean);
    }

    *//**
     * 根据id删除预约管理
     *
     * @param id
     * @return
     *//*
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    @RequestMapping("/delete.do")
    public Result delete(@RequestParam Long id) {
        log.info("要删除的预约管理id:{}", id);
        return checkItemService.clearCheckItem(id);
    }

    *//**
     * 编辑
     *
     * @param checkItem
     * @return
     *//*
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")//权限校验
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody CheckItem checkItem) {
        log.info("要编辑的预约管理:{}", checkItem);
        return checkItemService.editCheckItem(checkItem);
    }*/


}
